import Vue from 'vue'
import Vuex from 'vuex'
import zutil from '@/util/util'
import { zloginInfo } from '@/util/api';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		storecolor: ['#325B69', '#698570', '#AE5548', '#6D9EA8', '#9CC2B0', '#C98769'],
		hasLogin: false,
		loginUserInfo: null,
		w_hospital: null,
		w_shortageData: null
	},
	mutations: {
		setLogin (state, data) {
			state.hasLogin = data;
		},
		setLoginUserInfo (state, data) {
			state.loginUserInfo = data;
			zutil.lStorage.setParam('loginUserInfo', JSON.stringify(data));
		},
		setW_shortageData (state, data) {
			state.w_shortageData = data;
		},
		setW_hospital (state, data) {
			state.w_hospital = data;
		},
	},
	actions: {
		getPermissionList: async ({ state, commit }) => {
      // 这里是为了防止重复获取
      if (state.loginUserInfo) return;

      let loginUserInfo = zutil.lStorage.getParam('loginUserInfo');
      if(loginUserInfo){
      	loginUserInfo = JSON.parse(loginUserInfo);
      	commit('setLoginUserInfo', loginUserInfo);
      }else{
	      // 发送请求方法省略
	      const code = zutil.getQueryString("code") || "";
	      // 测试，需删
	      let res = await zloginInfo({code: code});
	      res = zutil.formatAxiosData(res);
	      if(res.stat == '0'){
	        commit('setLoginUserInfo', res.data);
	      }
	      /*if(code){
	        let res = await zloginInfo({code: code});
	        res = zutil.formatAxiosData(res);
	        if(res.stat == '0'){
		        commit('setLoginUserInfo', res.data);
	        }
	      }else{
	      	console.log('非法进入');
	      }*/
      }
    }
	}
})

export default store
